package com.tgra;

import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;

public class First3D_Core implements ApplicationListener
{
	MovingCamera movCam;
//	Sphere sphere;
	Cube cube;
	Skybox skybox;
	Pyramid pyramid;
	MovingSphere movSphere;
	
	float elapsedTime;
	
	float angle;
	
	@Override
	public void create()
	{
		Gdx.gl11.glEnable(GL11.GL_LIGHTING);
		Gdx.gl11.glEnable(GL11.GL_LIGHT0);
		Gdx.gl11.glEnable(GL11.GL_LIGHT1);
		Gdx.gl11.glEnable(GL11.GL_DEPTH_TEST);
		Gdx.gl11.glEnable(GL11.GL_TEXTURE_2D);
		Gdx.gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
		
		Gdx.gl11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		Gdx.gl11.glMatrixMode(GL11.GL_PROJECTION);
		Gdx.gl11.glLoadIdentity();
		Gdx.glu.gluPerspective(Gdx.gl11, 90, 1.333333f, 1.0f, 3000.0f);

		Gdx.gl11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

		movCam = new MovingCamera(new Point3D(400.0f, 200.0f, 400.0f), new Point3D(500.0f, 10.0f, 500.0f), new Vector3D(0.0f, 1.0f, 0.0f));
		cube = new Cube("first_texture.jpg");
		skybox = new Skybox("skybox_grass.png");
		pyramid = new Pyramid("roof_texture.jpg");
		movSphere = new MovingSphere(45, 90);
		
		elapsedTime = 0.0f;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	private void update()
	{		
		float deltaTime = Gdx.graphics.getDeltaTime();
		elapsedTime += deltaTime;
		angle += 30.0f*deltaTime;
/*
		if(Gdx.input.isKeyPressed(Input.Keys.UP))
		{
			cam.pitch(-90.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.DOWN))
		{
			cam.pitch(90.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
		{
			cam.yaw(-90.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
		{
			cam.yaw(90.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.W))
		{
			cam.slide(0.0f, 0.0f, -100.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.S))
		{
			cam.slide(0.0f, 0.0f, 100.0f * deltaTime);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.A))
		{
			cam.slide(-100.0f * deltaTime, 0.0f, 0.0f);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.D))
		{
			cam.slide(100.0f * deltaTime, 0.0f, 0.0f);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.R))
		{
			cam.slide(0.0f, 100.0f * deltaTime, 0.0f);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.F))
		{
			cam.slide(0.0f, -100.0f * deltaTime, 0.0f);
		}
		*/
		movSphere.update(elapsedTime);
		movCam.update(elapsedTime);

	}
	
	private void display()
	{
		Gdx.gl11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);

		movCam.setModelViewMatrix();
		
		float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, lightDiffuse, 0);

		float[] lightPosition = {5.0f, 10.0f, 15.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT0, GL11.GL_POSITION, lightPosition, 0);

		float[] lightDiffuse1 = {1.0f, 1.0f, 1.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, lightDiffuse1, 0);

		float[] lightPosition1 = {-5.0f, -10.0f, -15.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition1, 0);

		float[] materialDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
		Gdx.gl11.glMaterialfv(GL11.GL_FRONT, GL11.GL_DIFFUSE, materialDiffuse, 0);

		float[] lightAmbience1 = {9.0f, 9.0f, 9.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT0, GL11.GL_AMBIENT , lightAmbience1, 0);
		
		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(500.0f, 500.0f, 500.0f);
		Gdx.gl11.glScalef(1000.0f, 1000.0f, 1000.0f);
		skybox.draw();
		Gdx.gl11.glPopMatrix();
		
		drawHouse();
		movSphere.display();
	}
	
	public void drawHouse()
	{
		float scale_of_House = 50.f;
		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(500.0f, scale_of_House, 500.0f);
		Gdx.gl11.glScalef(scale_of_House, scale_of_House, scale_of_House);
//		Gdx.gl11.glRotatef(angle, 0.0f, 1.0f, 0.0f);
		pyramid.draw();
		Gdx.gl11.glPopMatrix();
	}

	@Override
	public void render()
	{
		update();
		display();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

}
